package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.QueryDto;
import com.zzyl.nursing.dto.TaskDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.TaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 护理任务Controller
 * 
 * @author ruoyi
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务的接口")
public class NursingTaskController extends BaseController
{
    @Autowired
    private INursingTaskService nursingTaskService;

    /**
     * 查询护理任务列表
     */
    @ApiOperation("查询护理任务列表")
    @GetMapping("/list")
    public TableDataInfo list(QueryDto dto)
    {
        startPage();
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(dto);
        return getDataTable(list);
    }

    /**
     * 新增护理任务
     */
    @ApiOperation("新增护理任务")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "护理任务实体")
            @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @ApiOperation("修改护理任务")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "护理任务实体")
            @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @ApiOperation("删除护理任务")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }

    /**
     * 取消护理任务
     */
    @ApiOperation("取消护理任务")
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody TaskDto dto)
    {
        //封装护理任务实体（设置要更新的字段）
        NursingTask nursingTask = new NursingTask();
        // 主键（必须，用于定位数据）
        nursingTask.setId(dto.getTaskId());
        //状态改为3（关闭）
        nursingTask.setStatus(3);
        // 取消原因
        nursingTask.setCancelReason(dto.getReason());

        nursingTaskService.updateById(nursingTask);
      return success();
    }


    /**
     * 执行护理任务
     *
     */
    @ApiOperation("执行护理任务")
    @PutMapping("/do")
    public AjaxResult doTask(@RequestBody TaskDto dto){

        //封装护理任务实体（设置要更新的字段）
        NursingTask nursingTask = new NursingTask();
        // 主键（必须，用于定位数据）
        nursingTask.setId(dto.getTaskId());

        nursingTask.setStatus(2);
        LocalDateTime estimatedServerTime = dto.getEstimatedServerTime();

        if(estimatedServerTime.isBefore(LocalDateTime.now())){
            throw new RuntimeException("执行任务时间不能早于当前时间");
        }
        //执行时间
        nursingTask.setEstimatedServerTime(estimatedServerTime);

        //实际服务时间
        nursingTask.setRealServerTime(LocalDateTime.now());
        //执行记录
        nursingTask.setMark(dto.getMark());

        //任务图片
        nursingTask.setTaskImage(dto.getTaskImage());


        nursingTaskService.updateById(nursingTask);



        return success();
    }

    /**
     * 修改护理任务执行时间
     */
    @ApiOperation("修改护理任务执行时间")
    @PutMapping("/updateTime")
    public AjaxResult updateTime(@RequestBody TaskDto dto){

        //封装护理任务实体（设置要更新的字段）
        NursingTask nursingTask = new NursingTask();

        // 主键（必须，用于定位数据）
        nursingTask.setId(dto.getTaskId());
        LocalDateTime estimatedServerTime = dto.getEstimatedServerTime();

        if(estimatedServerTime.isBefore(LocalDateTime.now())){
            throw new RuntimeException("期望服务时间不能早于当前时间");
        }
        //执行时间
        nursingTask.setEstimatedServerTime(estimatedServerTime);

        nursingTaskService.updateById(nursingTask);
        return success();
    }

    /**
     * 获取护理任务详细信息
     */
    @ApiOperation("获取护理任务详细信息")
    @GetMapping("{id}")
    public AjaxResult getTaskById(@PathVariable Long id){
        TaskVo taskVo = nursingTaskService.selectNursingTaskById(id);
        return success(taskVo);
    }
}
